﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using QuickApp.Core;
using Resto.CostControl.Core.Models;
using QuickApp.MVCControl;
using Resto.CostControl.Core;

namespace Resto.CostControl.Web.Controllers
{
    public class DeliveryPlanController : Controller
    {
        private readonly IRepository<MaterialDeliveryPlan> _Repo;
        private readonly IUnitOfWork _UnitOfWork;

       public DeliveryPlanController(IRepository<MaterialDeliveryPlan> repo, UnitOfWork unitOfWork)
        {
            _Repo = repo;
            _UnitOfWork = unitOfWork;
        }

        public ActionResult Index()
        {
            return View();
        }
        
        [HttpPost]
        public JsonResult List(DWZPageInfo pageInfo, DWZQueryModel where)
        {
            //where.AddItem("Week", CommonController.GetWeekOfYear(DateTime.Now).ToString());
            var list = _Repo.GetPageable(pageInfo.ToPageInfo(), where);
            var fdata = pageInfo.ToFlexiGridData<MaterialDeliveryPlan>(list.Page.ToList(), list.Total);
            return Json(fdata);
        }

        public ActionResult SetectType()
        {
            var types = (from plan in _Repo.FindAll()
                         select new DPDeliveryTypeViewModel
                         {
                             SiteId = plan.SiteId,
                             Date = plan.Date,
                         }).Distinct().ToList();

            foreach (var type in types)
            {
                type.SiteName = DependencyResolver.Current.GetService<IEnumService>().GetEnumText("ProjectSite", type.SiteId);
            }
            return View(types);
        }
    }
    public class DPDeliveryTypeViewModel
    {
        public string SiteId { get; set; }
        public string SiteName { get; set; }
        public DateTime Date { get; set; }
        public string DateString { get { return Date.ToString("yyyy-MM-dd"); } }
    }
}

